import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/number/numberSerial'

const api = {

    /**
     * 新增编号规则流水号
     * @param numberSerial
     * @returns {Promise<*|undefined>}
     */
    createNumberSerial: (numberSerial) =>
      axiosClient().post(`${requestUrl}/addInfo`, numberSerial),

    /**
     * 根据ID删除编号规则流水号
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteNumberSerialById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新编号规则流水号
     * @param id
     * @param numberSerial
     * @returns {Promise<*|undefined>}
     */
    updateNumberSerialFull: (id, numberSerial) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, numberSerial),

    /**
     * 根据ID部分更新编号规则流水号
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateNumberSerialPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询编号规则流水号
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getNumberSerialById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询编号规则流水号
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNumberSerialPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询编号规则流水号
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNumberSerialList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.ruleId) {
            params.append('ruleId', businessParam.ruleId)
        }
        if (businessParam.orgId) {
            params.append('orgId', businessParam.orgId)
        }
        if (businessParam.currentNum) {
            params.append('currentNum', businessParam.currentNum)
        }
        if (businessParam.minNum) {
            params.append('minNum', businessParam.minNum)
        }
        if (businessParam.maxNum) {
            params.append('maxNum', businessParam.maxNum)
        }
        if (businessParam.resetCycle) {
            params.append('resetCycle', businessParam.resetCycle)
        }
        if (businessParam.lastResetTime) {
            params.append('lastResetTime', businessParam.lastResetTime)
        }
        if (businessParam.format) {
            params.append('format', businessParam.format)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
